package com.cas.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cas.dao.CasDirectoryStructureMapper;
import com.cas.entity.CasDirectoryStructureDO;
import com.cas.service.BlogManagementService;
import com.cas.utils.IdGenerator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @description:
 * @author: xianglong
 * @create: 2025-02-21 09:43
 * 期望是文章的输出由后台的表控制，先将数据用表存储，然后部署到前端
 *
 **/
@Service
public class BlogManagementServiceImpl implements BlogManagementService {

    @Resource
    private CasDirectoryStructureMapper structureMapper;

    public void createDirectoryStructure(String path) {
        String[] parts = path.split("/");
        String parentId = null;

        for (int i = 0; i < parts.length; i++) {
            String name = parts[i];
            boolean isLeaf = (i == parts.length - 1);
            QueryWrapper<CasDirectoryStructureDO> wrapper = Wrappers.query();
            wrapper.eq("name", name);
            CasDirectoryStructureDO directory = structureMapper.selectOne(wrapper);
            if (directory == null) {
                directory = new CasDirectoryStructureDO();
                String id = IdGenerator.generateRandomNumber(16);
                directory.setId(id);
                directory.setName(name);
                directory.setParentId(parentId);
                directory.setLeaf(isLeaf);
                directory.setCreateTime(LocalDateTime.now());
                directory.setUpdateTime(LocalDateTime.now());
                structureMapper.insert(directory);
                parentId = id;
            } else {
                parentId = directory.getId();
            }
        }
    }

    @Override
    public void create() {
        // 1、构建文章的所属目录
        createDirectoryStructure("MP/A/C");

        // 2、组织文章生成提示词

        // 3、dk生成文章内容

        // 4、数据回填DB

    }

    @Override
    public void addOrUpdate() {
        // 1、获取DB数据源

        // 2、更新github仓库文件

        // 3、add提交本地

        // 4、push

    }

    @Override
    public void push() {

    }

    @Override
    public void deploy() {

    }
}
